package online.longzipeng.mywebdemo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.mywebdemo.entity.PushMessage;
import online.longzipeng.mywebdemo.entity.User;
import online.longzipeng.mywebdemo.enums.BaseEnum;
import online.longzipeng.mywebdemo.enums.PushMessageTypeEnum;
import online.longzipeng.mywebdemo.mapper.UserMapper;
import online.longzipeng.mywebdemo.service.PushMessageService;
import online.longzipeng.mywebdemo.service.UserService;
import online.longzipeng.mywebdemo.utils.MockUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lzp
 * @description: 批量插入案例
 * @Date: 2022/12/6
 */
@Slf4j
@SpringBootTest(classes = MyWebDemoApplication.class,
		// 配置端口启动，否则获取失败
		webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BatchInsertDemo {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserService userService;

	@Test
	public void testBatchInsert() throws ScriptException {
		List<User> users = CollUtil.newArrayList(
				new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()),
				new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()));

//		userMapper.batchInsert(users);
		userService.saveBatch(users);
	}

	/**
	 * 批量插入 循环测试    1000条数据 813 微秒     10000条数据 1764微秒
	 */
	@Test
	public void testBatchInsert2() throws ScriptException {
		List<User> users = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
			users.add(new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()));
		}
		long start = System.currentTimeMillis();
		userMapper.batchInsert(users);
		long costTime = (System.currentTimeMillis() - start);
		System.out.println("共花费了 " + costTime + "微秒");
	}

	/**
	 * 逐条插入 循环测试 1000条数据 1832微秒  10000条 9289微秒
	 */
	@Test
	public void testBatchInsert3() throws ScriptException {
		List<User> users = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
			users.add(new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()));
		}
		long start = System.currentTimeMillis();
		userService.saveBatch(users);
		long costTime = (System.currentTimeMillis() - start);
		System.out.println("共花费了 " + costTime + "微秒");
	}

	/**
	 * 不同格式的数据逐条插入
	 */
	@Test
	public void testBatchInsert4() throws ScriptException {
		List<User> users = CollUtil.newArrayList(
				new User(null, MockUtils.cname(), null, RandomUtil.randomInt(1, 3), null, new Date()),
				new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()),
				new User(null, MockUtils.cname(), null, RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()),
				new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), null, null),
				new User(null, MockUtils.cname(), RandomUtil.randomInt(18, 51), RandomUtil.randomInt(1, 3), MockUtils.name(), new Date()));

//		userMapper.batchInsert(users);
		userService.saveBatch(users);
	}


}
